import React, { useEffect, useState } from "react";

/**
 * 通用的过滤hook
 */
export default (props) => {
  const [currentData, setCurrentData] = useState([]);
  const [total, setTotal] = useState(0);

  const [isLoading, setIsLoading] = useState(false);

  const [paginationParams, setPaginationParams] = useState({
    order: "createTime",
    sort: "desc",
    page: 1,
    size: 4,
  }); 

  const [filterParams, setFilterParams] = useState({});

  const getData = async () => {
    console.log(props);
    setIsLoading(true);
    const data = await props.api({
      ...paginationParams,
      ...filterParams,
    });
    console.log(data);
    data.list.forEach((item) => {
      item.key = item.id;
    });
    console.log(data);
    // 必须所有的列表接口都要返回这种格式
    setCurrentData(data.list);
    setTotal(data.pagination.total);
    setIsLoading(false);
  };

  useEffect(() => {
    getData();
    console.log("filter hook useEffect on!!!!");
  }, [paginationParams, filterParams]);

  return {
    currentData,
    total,
    setPaginationParams,
    setFilterParams,
    getData,
    isLoading,
    paginationParams,
  };
};
